//
//  HSYProgressView.m
//  高仿暴走斗图
//
//  Created by key on 16/5/10.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYProgressView.h"
#import "HSYProgressSliderView.h"

@interface HSYProgressView()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIView *whiteView;
@property (nonatomic, strong) UIView *greenView;
@property (nonatomic, strong) HSYProgressSliderView *sliderView;
@property (nonatomic, assign) CGPoint begin;
@property (nonatomic, assign) CGPoint last;

@property (nonatomic, assign) BOOL isCanMoving;
@end
@implementation HSYProgressView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.isCanMoving = NO;
        
        self.backgroundColor = [UIColor brownColor];
        
        self.whiteView = [NSObject createViewWithBgColor:[UIColor whiteColor]];
        self.whiteView.clipsToBounds = YES;
        [self.whiteView.layer setCornerRadius:self.whiteView.viewHeight/2];
        self.whiteView.layer.borderColor = [UIColor greenColor].CGColor;
        self.whiteView.layer.borderWidth = 0.3f;
        [self addSubview:self.whiteView];
        self.whiteView.frame = CGRectMake(26, 30, self.viewWidth - 52, 25.0f);
        
        
        self.greenView = [NSObject createViewWithBgColor:[UIColor greenColor]];
        self.greenView.frame = self.sliderView.bounds;
        [self.whiteView addSubview:self.greenView];
        
        self.sliderView = [[HSYProgressSliderView alloc] initWithFrame:CGRectMake(1.0, 0, 52.0f, 52.0f)];
        self.sliderView.backgroundColor = [UIColor grayColor];
        [self addSubview:self.sliderView];
        
        self.begin = self.sliderView.frame.origin;
        [self addPanGesture];
    }
    return self;
}

- (void)addPanGesture {
    
    //添加滑动手势
    UIPanGestureRecognizer *panGesture = [UIView addGestureRecognizerWithGesturesType:kGesturesTypePanGestureRecognizer action:@selector(panGestureRecognizer:) delegate:self];
    panGesture.delegate = self;
    [self addGestureRecognizer:panGesture];
}

- (void)panGestureRecognizer:(UIPanGestureRecognizer *)panGesture {
    
    if (!self.isCanMoving) {
        return;
    }
    self.last = [panGesture locationInView:self];
    switch (panGesture.state) {
        case UIGestureRecognizerStateBegan:
            
            break;
            
        case UIGestureRecognizerStateChanged:{
            
            CGFloat offset = self.last.x - self.sliderView.sliderX;
            NSLog(@"offset :%f", offset);
            if (offset + self.sliderView.viewWidth > self.viewWidth) {
                
                NSLog(@"向右时 return");
                return; //向右
                
            } else if (offset < 0.0f) {
                
                NSLog(@"向左时 return");
                return; //向左
                
            }
            self.sliderView.percentage = offset/self.viewWidth;
            [self.sliderView setViewPoint:CGPointMake(offset, self.sliderView.viewY)];
        }
            break;
            
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateFailed:
            
            
            self.begin = CGPointMake((self.sliderView.viewX), 0);
            NSLog(@"begin: %@", NSStringFromCGPoint(self.begin));
            
            break;
            
        default:
            break;
    }
}


#pragma mark - Gesture recognizer delegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        //滑动类型
        UIPanGestureRecognizer *panGestureRecognizer = (UIPanGestureRecognizer *)gestureRecognizer;
        CGPoint translation = [panGestureRecognizer translationInView:[self superview]];
//        CGPoint point = [panGestureRecognizer locationInView:self];
//        if (point.x >= (CGRectGetMinX(self.sliderView.frame)) && point.x <= (CGRectGetMaxX(self.sliderView.frame) )) {
//            
//            self.isCanMoving = YES;
//        } else {
//            
//            self.isCanMoving = NO;
//        }
        return (fabs(translation.x) / fabs(translation.y) > 1) ? YES : NO;
        
    } else {
        
        return NO;
    }
}

#pragma mark - Touch 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    if (point.x >= (CGRectGetMinX(self.sliderView.frame)) && point.x <= (CGRectGetMaxX(self.sliderView.frame))) {
        
        self.isCanMoving = YES;
    } else {
        
        self.isCanMoving = NO;
    }
}
@end
